﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace lapinou_game2
{
    class Projectile
    {
        ContentManager content;
        GraphicsDeviceManager graphics;

        Vector2 poscurseur;
        Vector2 poslapin;
        Vector2 proj;

        Texture2D mr_tex;
        Texture2D ml_tex;

        string rab_direction = "right";

        float ar = 0;
        float al = 0;
        float b = 0;
        float taille;
        float largeur;
        float montevit;
        float rotation;


        public Projectile(ContentManager Content, GraphicsDeviceManager Graphics, Vector2 Poslapin, string rab_direction, Vector2 Poscurseur, float Taille, float Largeur)
        {
            proj = Poslapin;
            taille = Taille;
            largeur = Largeur;
            poslapin = Poslapin;
            poscurseur = Poscurseur;
            content = Content;
            graphics = Graphics;
            this.rab_direction = rab_direction;

        }
        public void LoadContent(ContentManager Content)
        {
            mr_tex = content.Load<Texture2D>(@"image\missile_r");
            ml_tex = content.Load<Texture2D>(@"image\missile_l");
        }

        public void Initialize(Cursor cursor, Vector2 poslapin)
        {
            poscurseur = cursor.getPos(); //met a jour la position du curseur
            ar = (poscurseur.Y - poslapin.Y) / (poscurseur.X - poslapin.X - largeur / 2); // y = ax +b
            al = (poscurseur.Y - poslapin.Y) / (poscurseur.X - poslapin.X + largeur / 2);
            b = poslapin.Y + taille / 2;

            if (rab_direction == "right")
            {
                proj.X = poslapin.X + largeur / 3 + 10;
                proj.Y = ar * 0 + b;
            }
            else if (rab_direction == "left")
            {
                proj.X = poslapin.X + largeur / 3 + 20;
                proj.Y = al * 0 + b;
            }
            else if (rab_direction == "haut")
            {
                proj.X = poslapin.X;
                proj.Y = ar * 0 + b;
            }
            else
            {
                proj.X = poslapin.X;
                proj.Y = ar * 0 + b;
            }


            montevit = 0; //PAS TOUCHE
            rotation = Convert.ToInt64(Math.Acos(Convert.ToDouble((poscurseur.X - poslapin.X)) / (Math.Sqrt(Convert.ToDouble((poscurseur.X - poslapin.X) * (poscurseur.X - poslapin.X) + (poscurseur.Y - poslapin.Y + taille / 2) * (poscurseur.Y - poslapin.Y + taille / 2))))));

        }

        public void UpDate(Cursor cursor)
        {
            poscurseur = cursor.getPos();

            if (rab_direction == "right")
            {

                proj.X = proj.X + 20; // JE FAIS JUSTE UNE FONCTION AFFINE
                proj.Y = ar * montevit + b; // PAS TOUCHE
                montevit += 20; // PAS TOUCHE
            }
            else if (rab_direction == "left")
            {
                proj.X = proj.X - 20;
                proj.Y = -al * montevit + b;
                montevit += 20;
            }
            else if (rab_direction == "haut")
            {
                proj.X = poslapin.X + largeur / 2;
                proj.Y = poslapin.Y - taille / 2 + montevit;
                montevit -= 20;
            }
            else
            {
                proj.X = poslapin.X + largeur / 2;
                proj.Y = poslapin.Y + montevit;
                montevit += 20;
            }
        }

        public bool screen()
        {
            if ((proj.X > graphics.PreferredBackBufferWidth) || (proj.Y > graphics.PreferredBackBufferHeight))
            {
                return (false);
            }
            else
            {
                return (true);
            }

        }

        public void Draw(SpriteBatch sb)
        {

            if (rab_direction == "right")
                if (ar < 0.45f)
                    sb.Draw(mr_tex, proj, null, Color.White, -rotation, Vector2.Zero, new Vector2(0.5f, 0.5f), SpriteEffects.None, 0);
                else
                    sb.Draw(ml_tex, proj, null, Color.White, -rotation - 1.57f, Vector2.Zero, new Vector2(0.5f, 0.5f), SpriteEffects.None, 0);
            else if (rab_direction == "left")
                if (al > -0.7f)
                    sb.Draw(ml_tex, proj, null, Color.White, -rotation - 3.14f, Vector2.Zero, new Vector2(0.5f, 0.5f), SpriteEffects.None, 0);
                else
                    sb.Draw(ml_tex, proj, null, Color.White, -rotation - 4.71f, Vector2.Zero, new Vector2(0.5f, 0.5f), SpriteEffects.None, 0);
            else if (rab_direction == "haut")
                sb.Draw(mr_tex, proj, null, Color.White, -1.57f, Vector2.Zero, new Vector2(0.5f, 0.5f), SpriteEffects.None, 0);
            else
                sb.Draw(mr_tex, proj, null, Color.White, 1.57f, Vector2.Zero, new Vector2(0.5f, 0.5f), SpriteEffects.None, 0);
        }

    }
}